﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SyndicateBusiness.Classes;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Classes
{
    public class BasePage : System.Web.UI.Page
    {
        public string PageTitle
        {
            get;
            set;
        }

        public virtual BusinessFacade BusinessFacade
        {
            get
            {
                if (MasterPage == null) return new BusinessFacade();
                else return MasterPage.BusinessFacade;
            }
        }

        public BigLotterySyndicate.SecuredSite MasterPage
        {
            get
            {
                return this.Master as BigLotterySyndicate.SecuredSite;
            }
        }

        public void Redirect(string url)
        {

            HttpContext.Current.Response.Redirect(url);
        }

        public void Redirect(Uri uri)
        {
            this.Redirect(uri.ToString());
        }

        public void Refresh()
        {
            this.Redirect(this.Request.Url.AbsoluteUri);
        }

        public bool IsUserLoggedIn
        {
            get
            {
                return BusinessFacade.UserBL.IsLoggedIn;
            }
        }

        public int? LoggedInUserId
        {
            get
            {
                return BusinessFacade.UserBL.LoggedInUserId;
            }
        }

        public bool IsAdmin
        {
            get { return IsUserLoggedIn ? LoggedInUser.isAdmin : false; }
        }

        public string ReferrerUrl
        {
            get
            {
                return (string)this.ViewState["BasePage_ReferrerUrl"];
            }
            set
            {
                if (value == null)
                    this.ViewState.Remove("BasePage_ReferrerUrl");
                else
                    this.ViewState["BasePage_ReferrerUrl"] = value;
            }
        }

        public user LoggedInUser
        {
            get
            {
                return BusinessFacade.UserBL.LoggedInUser;
            }
        }

        public void GoLogoutAdmin()
        {
            Redirect("/Login/Logout.aspx");
        }

        public void GoLogout(string sitePrefix)
        {
            Redirect("/Login/Logout.aspx");
        }

        public void InsertEventLogEntry(string desc)
        {
            if (IsUserLoggedIn)
                this.BusinessFacade.EventLogBL.InsertEntry(LoggedInUser, desc);
        }

        public void InsertEventLogEntry(string desc, string type = eventLog.eventType.CHANGE)
        {
            if (IsUserLoggedIn)
                this.BusinessFacade.EventLogBL.InsertEntry(LoggedInUser, desc, type);
        }

    }
}